package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.mapper.GridInformationMapper;
import com.smart.village.big.screen.mapper.ShopMapper;
import com.smart.village.big.screen.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private GridInformationMapper gridInformationMapper;

    @Override
    public JSONObject getShopList(String townNumber, String villageNumber, String category, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> shopList = shopMapper.getShopList(townNumber,villageNumber,category);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(shopList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getShopDetail(String shopSerialNumber) {
        return shopMapper.getShopDetail(shopSerialNumber);
    }

    @Override
    public List<JSONObject> getShopListByCategory() {
        List<JSONObject> list = new ArrayList<JSONObject>();

        List<JSONObject> shopListByCategory = shopMapper.getShopListByCategory();
        shopListByCategory.forEach(shop -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("text",shop.containsKey("Merchant_Category")?shop.get("Merchant_Category").toString():"");
            jsonObject.put("value",shop.containsKey("Merchant_Category_SerialNumber")?shop.get("Merchant_Category_SerialNumber").toString():"");
            list.add(jsonObject);
        });
        return list;
    }

    @Override
    public JSONObject getShopCoordsList(String townNumber, String villageNumber, String category) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "FeatureCollection");
        JSONObject crs = new JSONObject();
        crs.put("type", "name");
        JSONObject crsP = new JSONObject();
        crsP.put("name", "EPSG:4490");
        crs.put("properties", crsP);

        jsonObject.put("crs", crs);
        List<JSONObject> list = shopMapper.getShopList(townNumber,villageNumber,category);
        List<JSONObject> features = Lists.newArrayList();
        for (JSONObject coords : list) {
            JSONObject feature = new JSONObject();
            feature.put("type", "Feature");
            feature.put("id", coords.getString("Shop_SerialNumber"));
            JSONObject geometry = new JSONObject();
            geometry.put("type", "point");
            geometry.put("coordinates", coords.getString("Coord").replace(" ", ""));
            feature.put("geometry", geometry);
            feature.put("properties", coords);
            if (!ObjectUtils.isEmpty(coords.getString("Coord")) && !coords.getString("Coord").replace(" ", "").equals("[]]")) {
                coords.remove("Coords");
                coords.remove("Coord");
                features.add(feature);
            }
        }
        jsonObject.put("features", features);
        return jsonObject;
    }

    @Override
    public JSONObject getEventCategory(String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> shopList = shopMapper.getEventCategory(townNumber,villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(shopList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getComplaintsDetail(String complaintId) {
        JSONObject complaintsDetail = gridInformationMapper.getComplaintsDetail(complaintId);
        if(ObjectUtils.isEmpty(complaintsDetail)){
            return new JSONObject();
        }
        List<JSONObject> complaintsDetailFullImage = gridInformationMapper.getComplaintsDetailFullImage(complaintId);
        complaintsDetail.put("complaintsDetailFullImage",complaintsDetailFullImage);
        return complaintsDetail;
    }

    @Override
    public List<JSONObject> getGlobalSearch() {
        return shopMapper.getGlobalSearch();
    }

    @Override
    public List<JSONObject> getShopSatisfaction(String townNumber, String villageNumber) {
        List<JSONObject> shopSatisfaction = shopMapper.getShopSatisfaction(townNumber,villageNumber);
        // 转换成 name value 的 数组
        List<JSONObject> list = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","不满意");
        jsonObject.put("value",shopSatisfaction.get(0).get("unsatisfact"));
        list.add(jsonObject);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name","满意");
        jsonObject1.put("value",shopSatisfaction.get(0).get("satisfact"));
        list.add(jsonObject1);
        return list;

    }

    @Override
    public JSONObject getShopRank(String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> shopList = shopMapper.getShopRank(townNumber,villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(shopList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getNewMedia(String townNumber, String villageNumber, String haveShop, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> shopList = shopMapper.getNewMedia(townNumber,villageNumber,haveShop);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(shopList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public List<JSONObject> getShopSatisfactionDetail(String townNumber, String villageNumber, String satisfaction) {
        return shopMapper.getShopSatisfactionDetail(townNumber,villageNumber,satisfaction);
    }
}
